//
//  LGSSelectNodeTypeSheetController.m
//  LogicSim
//
//  Created by Jeremy Pereira on 29/04/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LGSSelectNodeTypeSheetController.h"

enum  
{
    SHEET_END_OK = 0,
    SHEET_END_CANCEL = 1,
};

@interface LGSSelectNodeTypeSheetController()

-(void) didSelectNodeTypeSheet: (NSWindow*) sheet 
                    returnCode: (NSInteger) returnCode 
                   contextInfo: (void*) context;
@property (weak) id<LGSNodeTypeSelectorDelegate> delegate;

@end

@implementation LGSSelectNodeTypeSheetController

@synthesize selectNodeTypeSheet, nodeTypeListSelector, delegate;

-(void) showWithDelegate: (id<LGSNodeTypeSelectorDelegate>) newDelegate
{
    [self setDelegate: newDelegate];
    [[self nodeTypeListSelector] removeAllItems];
    for (int i = 0 ; i < [[self delegate] itemCountForNodeTypeSelector: self] ; ++i)
    {
        [[self nodeTypeListSelector] addItemWithTitle: 
         		[[self delegate] labelForNodeTypeSelector: self 
                	                              atIndex: i]];
    }
    [NSApp beginSheet: [self selectNodeTypeSheet]
       modalForWindow: [[self delegate] windowForNodeTypeSelector: self]
        modalDelegate: self 
       didEndSelector: @selector(didSelectNodeTypeSheet:returnCode:contextInfo:)
          contextInfo: NULL];
}

-(void) didSelectNodeTypeSheet: (NSWindow*) sheet 
                    returnCode: (NSInteger) returnCode 
                   contextInfo: (void*) context
{
    [sheet orderOut: self];
    if (returnCode == SHEET_END_OK)
    {
        [[self delegate] nodeTypeSelector: self 
                            selectedIndex: [[self nodeTypeListSelector] indexOfSelectedItem]];
    }
    else 
    {
        [[self delegate] nodeTypeSelectorCanceled: self];
    }
}

-(IBAction) closeSheet: (id)sender
{
    [NSApp endSheet: [self selectNodeTypeSheet] returnCode: SHEET_END_OK];
}

-(IBAction) cancelSheet: (id)sender
{
    [NSApp endSheet: [self selectNodeTypeSheet] returnCode: SHEET_END_CANCEL];
}


@end
